//
//  ViewedCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/7.
//

import UIKit

class ViewedCell: UITableViewCell {
    
    lazy var selBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "setting_feedback_chose_gray"), for: .normal)
        btn.setImage(UIImage(named: "library_book_sel"), for: .selected)
        return btn
    }()
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(2)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        return label
    }()
    
    lazy var authorLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_999999")
        return label
    }()
    
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .regular, size: 12)
        label.textColor = .init(named: "gray_999999")
        return label
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "gray_D0D0D0")
        return view
    }()
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
                authorLabel.text = m.authorname
                timeLabel.text = (m.readingTime)?.wn_toJavaDetailDate()
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(selBtn)
        contentView.addSubview(bookImage)
        contentView.addSubview(nameLabel)
        contentView.addSubview(authorLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(lineView)
        selBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(-22)
            make.width.height.equalTo(20)
        }
        bookImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(12)
            make.size.equalTo(CGSize(width: scaleWidth(61), height: scaleWidth(90)))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bookImage.snp.right).offset(10)
            make.top.equalTo(bookImage.snp.top).offset(0)
            make.right.equalTo(-12)
        }
        authorLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(nameLabel.snp.bottom).offset(8)
            make.right.equalTo(-12)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.bottom.equalTo(-12)
            make.right.equalTo(-12)
        }
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.bottom.right.equalTo(0)
            make.height.equalTo(0.5)
        }
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        selBtn.isSelected = selected
    }
    
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        for control in subviews {
            if control.isMember(of: NSClassFromString("UITableViewCellEditControl")!) {
                control.isHidden = true
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
